//
//  CommentsViewController.m
//  Tang
//
//  Created by 邓芮 on 15/12/9.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "CommentsViewController.h"
@interface CommentsViewController ()
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *picBtn;

@property (nonatomic, strong) NSString *tableForTitleName;
@end

@implementation CommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // self.indentierId = @"123";
    self.tableForTitleName = [NSString stringWithFormat:@"Comments%@",_indentierId];
    
    self.navigationItem.title = @"发表评论";
    UIButton *leftBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftItem;
    
    UIButton *rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [rightBtn setTitle:@"发表" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    self.navigationController.navigationBar.translucent = NO;

    self.view.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1];
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(10 * KWidth / 414, 10 * KWidth /414, KWidth - 20 * KWidth / 414, KHeight / 2)];
    self.textView.backgroundColor = [UIColor whiteColor];
    self.textView.font = [UIFont systemFontOfSize:30 * KWidth / 414];
    [self.view addSubview:self.textView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self.textView becomeFirstResponder];
    
}

- (void)keyBoardHide:(NSNotification *)notification
{
    self.textView.frame = CGRectMake(10 * KWidth / 414, 10 * KWidth / 414, KWidth - 20 * KWidth / 414, KHeight / 2);
    
}

- (void)KeyBoardShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat height = [value CGRectValue].size.height;
    self.textView.frame = CGRectMake(10 * KWidth / 414, 10 * KWidth / 414, KWidth - 20 * KWidth / 414, KHeight - 64 * KWidth / 414 - height - 50 * KWidth / 414);
}

#pragma mark -- 键盘消失 --
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.textView resignFirstResponder];
}

#pragma mark -- 返回 --
- (void)backAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- 发表 --
- (void)rightBtnAction:(UIButton *)button
{
    [self addConmmentsData];
    
}


- (void)addConmmentsData
{
    BmobUser *bmobUser = [BmobUser getCurrentUser];
    
    BmobObject *comments = [BmobObject objectWithClassName:_tableForTitleName];
    [comments setObject:bmobUser.username forKey:@"userName"];
    [comments setObject:_textView.text forKey:@"userComments"];
    [comments setObject:bmobUser.objectId forKey:@"author"];
    [comments saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error)
    {
        if (isSuccessful)
        {
            NSLog(@"评论成功");
            [self promtViewWithTitle:@"发布成功" buttonEnabled:self.navigationItem.rightBarButtonItem viewUser:self.view];
            self.textView.text = @"";
           
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [self dismissViewControllerAnimated:YES completion:nil];
            });
                
           
        }
        else
        {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    }];
    
    

}

- (void)alertActionWithTitle:(NSString *)title message:(NSString *)message other:(NSString *)other
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:otherAction];
    [self presentViewController:alertVc animated:YES completion:nil];
}

// 弹出提示窗口
- (void)promtViewWithTitle:(NSString *)title buttonEnabled:(UIBarButtonItem *)buttonEnabled viewUser:(UIView *)aView
{
    aView.userInteractionEnabled = NO;
    buttonEnabled.enabled = NO;
    UIView *promptView = [[UIView alloc] initWithFrame:CGRectMake(((KWidth - (KWidth / 2) / 2) * 2), 0, KWidth / 2, KWidth / 4)];
    promptView.backgroundColor = [UIColor whiteColor];
    promptView.alpha = 0;
    promptView.layer.cornerRadius = 10;
    promptView.layer.borderColor = [UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1].CGColor;
    promptView.layer.borderWidth = 1;
    UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(((KWidth - (KWidth / 2)) / 2) * 2, 0, KWidth / 2, KWidth / 4)];
    titlelabel.text = title;
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.textColor = [UIColor blackColor];
    titlelabel.font = [UIFont systemFontOfSize:KWidth / 25];
    titlelabel.alpha = 0;
    [self.view addSubview:promptView];
    [self.view addSubview:titlelabel];
    
    [UIView animateWithDuration:0.5 animations:^{
        promptView.alpha = 0.8;
        titlelabel.alpha = 0.8;
        promptView.frame = CGRectMake((KWidth - (KWidth / 1.2))/ 2, KWidth - KWidth / 1.35, KWidth /1.2, KWidth / 4);
        titlelabel.frame = CGRectMake((KWidth - KWidth / 1.2) / 2, KWidth - KWidth / 1.35, KWidth / 1.2, KWidth / 4);
        
        
    } completion:^(BOOL finished)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
        {
           [UIView animateWithDuration:0.4 animations:^{
               promptView.alpha = 0 ;
               titlelabel.alpha = 0;
               promptView.frame = CGRectMake(((KWidth - (KWidth / 2) / 2) * 2), 0, KWidth / 2, KWidth / 4);
               titlelabel.frame =CGRectMake (((KWidth - (KWidth / 2)) / 2) * 2, 0, KWidth / 2, KWidth / 4);
           } completion:^(BOOL finished)
            {
                buttonEnabled.enabled = YES;
                aView.userInteractionEnabled = YES;
                [promptView removeFromSuperview];
                [titlelabel removeFromSuperview];
           }];
            
        });
      
    }];
}
     
    





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
